// pages/modify/modify.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    nickname: '',
    name: '',
    phone: '',
    emerName: '',
    emerPhone: '',
    userAvatar: ''

  },

  handleName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  handlePhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  handleEmerName: function (e) {
    this.setData({
      emerName: e.detail.value
    })
  },

  handleEmerPhone: function (e) {
    this.setData({
      emerPhone: e.detail.value
    })
  },

  handleCommit: function (e) {

    let name = this.data.name;
    let phone = this.data.phone;
    let emergencyName = this.data.emerName;
    let emergencyPhone = this.data.emerPhone;

    if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(name))) {
      wx.showToast({
        title: '真实姓名格式有误',
        duration: 2000,
        icon: 'none'
      });
      return false;
    }

    if (!(/^1[345768]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '联系电话格式有误',
        duration: 2000,
        icon: 'none'
      });
      return false;
    }

    if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(emergencyName))) {
      wx.showToast({
        title: '紧急联系人姓名格式有误',
        duration: 2000,
        icon: 'none'
      });
      return false;
    }

    if (!(/^1[345768]\d{9}$/.test(emergencyPhone))) {
      wx.showToast({
        title: '紧急联系人电话格式有误',
        duration: 2000,
        icon: 'none'
      });
      return false;
    }

    wx.showLoading({
      title: '修改中',
    })

    wx.request({
      url: app.globalData.prefix+'/visitor/modifyPersonalInfo',
      method: "POST",
      data: {
        id: wx.getStorageSync('userId'),
        name: this.data.name,
        phone: this.data.phone,
        emergencyName: this.data.emerName,
        emergencyPhone: this.data.emerPhone
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code === 0) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000,
            success: () => {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 0,
                })
              }, 1000)
            }
          })
        }
      }
    })



  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let userId = wx.getStorageSync('userId');

    //目前的个人信息
    wx.request({
      url: app.globalData.prefix+'/visitor/getVisitorById/' + userId,
      success: (res) => {
        var p = res.data.data;
        this.setData({
          nickname: p.userInfo.userNickname,
          name: p.userInfo.userName,
          phone: p.userInfo.userPhone,
          emerName: p.visitorInfo.emergencyName,
          emerPhone: p.visitorInfo.emergencyPhone,
          userAvatar: p.userInfo.userAvatar
        })
      }
    })


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})